package com.mmy.controller.oa;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.mmy.domain.oa.MaterialBase;
import com.mmy.domain.oa.MaterialOut;
import com.mmy.domain.oa.VMaterialOut;
import com.mmy.domain.sys.TSysUser;
import com.mmy.service.oa.impl.MaterialBaseServiceImpl;
import com.mmy.service.oa.impl.MaterialOutServiceImpl;

/**
 * 类的介绍：物料出库
 *
 * @author shangzl
 * @date 创建日期：
 * @version 1.0
 */
@Controller
public class MaterialOutController {

	@Autowired
	MaterialOutServiceImpl materialOutServiceImpl;
	@Autowired
	MaterialBaseServiceImpl materialBaseServiceImpl;

	/**
	 * 函数功能说明： 跳转
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 shangzl
	 * @date 创建日期 2020-07-30
	 */
	@RequestMapping(value = "/materialout")
	public String ToMaterialOut(Model model) {
		try {

			List<VMaterialOut> lstMaterialOut = materialOutServiceImpl.findAll();
			model.addAttribute("moulist", lstMaterialOut);
			List<MaterialBase> lstMaterialBase = materialBaseServiceImpl.findAll();
			model.addAttribute("mblist", lstMaterialBase);
			return "OA/material/materialout";
		} catch (Exception e) {

		}
		return null;

	}

	@RequestMapping(value = "/creatematerialout")
	public String ToCreateMaterialOut(Model model) {

		List<MaterialBase> lstMaterialBase = materialBaseServiceImpl.findAll();
		model.addAttribute("mblist", lstMaterialBase);
		return "OA/material/creatematerialout";
	}

	/**
	 * 函数功能说明： 新建物料出库
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 shangzl
	 * @date 创建日期 2020-08-03
	 */

	@RequestMapping(value = "/subcreatematerialout")
	public String CreateMaterialOut(MaterialOut materialOut, MaterialBase materialBase, HttpSession session)
			throws Exception {
//		获取最大值
		Object oOutID = materialOutServiceImpl.getMaxOutID();
//		 判断数据库是否存在菜单id 如果存在在最大值基础上加一 如果不存在把最大值置为一 
		if (oOutID == null) {
			materialOut.setOutID(1);

		} else {
			materialOut.setOutID(Integer.parseInt(oOutID.toString()) + 1);
		}


		try {
			materialOut.setCreateDate(new Date());
			TSysUser tSysUser = (TSysUser) session.getAttribute("tSysUser");
			materialOut.setCreator(tSysUser.getUserName());
			materialOutServiceImpl.save(materialOut.getOutID());
			materialOutServiceImpl.save(materialOut);
			int iMaterialID = materialOut.getMaterialID();
			materialBase = materialBaseServiceImpl.findByMaterialID(iMaterialID);
			int iNumber = materialBase.getNumber() - materialOut.getOutNumber();
			if (iNumber < 0) {
				return "test";
			}
			materialBaseServiceImpl.updateNumberByMaterialID(iNumber, iMaterialID);
			return "redirect:materialout";

		} catch (Exception e) {

		}
		return null;
	}

	/**
	 * 函数功能说明： 删除物料入库
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 shangzl
	 * @date 创建日期 2020-08-03
	 */

	@PostMapping("/delmaterialout")
	@ResponseBody
	public String DelMaterialOut(String checkList) {

		String[] checklist = checkList.split(",");
		List<Integer> lstOutID = new ArrayList<>();

		for (String str : checklist) {
			lstOutID.add(Integer.parseInt(str));
		}

		materialOutServiceImpl.delMaterialOut(lstOutID);

		return "OA/material/materialout";
	}

	/**
	 * 函数功能说明： 修改物料入库
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 shangzl
	 * @date 创建日期 2020-08-03
	 */

	@RequestMapping(value = "/enterupdatematerialout")
	public String EnterUpdateMaterialOut(HttpServletRequest request, Model model) {

		int iOutID = Integer.parseInt(request.getParameter("iOutID"));

		MaterialOut materialOut = materialOutServiceImpl.findByOutID(iOutID);
		model.addAttribute("materialOut", materialOut);
		List<MaterialBase> lstMaterialBase = materialBaseServiceImpl.findAll();
		model.addAttribute("mblist", lstMaterialBase);
		return "OA/material/updatematerialout";

	}

	@RequestMapping(value = "/updatematerialout")
	public String UpdateMaterialOut(MaterialOut materialOut, HttpSession session) throws Exception {

		try {
			/* 定义一个新的实体类对象 */
			MaterialOut dmaterialOut = materialOutServiceImpl.findByOutID(materialOut.getOutID());
			int iMaterialID = dmaterialOut.getMaterialID();
			MaterialBase materialBase = materialBaseServiceImpl.findByMaterialID(iMaterialID);
			int iNumber = materialBase.getNumber() + dmaterialOut.getOutNumber() - materialOut.getOutNumber();
			if (iNumber < 0) {
				return "test";
			}
			dmaterialOut.setMaterialID(materialOut.getMaterialID());
			dmaterialOut.setOutNumber(materialOut.getOutNumber());
			dmaterialOut.setMemo(materialOut.getMemo());
			dmaterialOut.setModifyDate(new Date());
			TSysUser tSysUser = (TSysUser) session.getAttribute("tSysUser");
			dmaterialOut.setReviser(tSysUser.getUserName());
			materialBaseServiceImpl.updateNumberByMaterialID(iNumber, iMaterialID);
			materialOutServiceImpl.save(dmaterialOut);
			return "redirect:materialout";

		} catch (Exception e) {

		}
		return null;
	}

	@RequestMapping(value = "/entercheckupdatematerialout")
	public String EnterCheckUpdateMaterialOut(HttpServletRequest request, Model model) {

		int iOutID = Integer.parseInt(request.getParameter("iOutID"));

		MaterialOut materialOut = materialOutServiceImpl.findByOutID(iOutID);
		model.addAttribute("materialOut", materialOut);
		String strMaterialName = materialBaseServiceImpl.findByMaterialID(materialOut.getMaterialID())
				.getMaterialName();
		model.addAttribute("strMaterialName", strMaterialName);
		return "OA/material/checkupdatematerialout";

	}

	@RequestMapping(value = "/checkupdatematerialout")
	public String CheckUpdateMaterialOut(MaterialOut materialOut) throws Exception {

		try {
			/* 定义一个新的实体类对象 */
			MaterialOut dmaterialOut = materialOutServiceImpl.findByOutID(materialOut.getOutID());
			dmaterialOut.setMaterialID(materialOut.getMaterialID());
			dmaterialOut.setOutNumber(materialOut.getOutNumber());
			dmaterialOut.setConfirm(materialOut.getConfirm());
			dmaterialOut.setConfirmDate(new Date());
			dmaterialOut.setMemo(materialOut.getMemo());
			materialOutServiceImpl.save(dmaterialOut);
			return "redirect:materialout";

		} catch (Exception e) {

		}
		return null;
	}

	/**
	 * 函数功能说明： 查询物料入库信息
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 shangzl
	 * @date 创建日期 2020-08-03
	 */
	@RequestMapping(value = "/searchmaterialout")
	public String FindMaterialOut(HttpServletRequest request, Model model) {

		String strMaterialName = request.getParameter("MaterialName");
		if (strMaterialName == "") {
			List<VMaterialOut> lstMaterialOut = materialOutServiceImpl.findAll();
			model.addAttribute("moulist", lstMaterialOut);
			List<MaterialBase> lstMaterialBase = materialBaseServiceImpl.findAll();
			model.addAttribute("mblist", lstMaterialBase);
			return "OA/material/materialout";
		} else {
			List<VMaterialOut> lstMaterialOut = materialOutServiceImpl.findByMaterialName(strMaterialName);
			model.addAttribute("moulist", lstMaterialOut);
			List<MaterialBase> lstMaterialBase = materialBaseServiceImpl.findAll();
			model.addAttribute("mblist", lstMaterialBase);
			return "OA/material/materialout";
		}

	}
}
